"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/11/13 10:21
@Description :   用户的登录注册
"""
import pymysql

conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    passwd='root',
    db='python2514'
)
#  游标对象
cur = conn.cursor()

welcome_info = """
    ----------------------------------
    1.登录
    2.注册
    ----------------------------------
"""
while True:
    #  打印欢迎词
    print(welcome_info)
    user_choice = input("请选择:")
    if user_choice == '1':
        print('------------欢迎登录-------------')
        username = input("请输入用户名:")
        password = input("请输入密码:")
        #  sql 注入    select * from user where username = "user3" -- " and password = "111111"
        # str_sql = f'select * from user where username = "{username}" and password = "{password}"'
        str_sql = 'select * from user where username = %s and password = %s'
        print(str_sql)
        line = cur.execute(str_sql, (username, password))
        if line:
            print("认证通过")
        else:
            print("用户名或密码错误")
    elif user_choice == '2':
        print('------------欢迎注册-------------')
        username = input("请输入用户名:")
        password = input("请输入密码:")
        str_sql = f"insert into user (username, password) values ('{username}','{password}')"
        line = cur.execute(str_sql)
        conn.commit()
        if line:
            print("注册成功")
        else:
            print("注册失败")
    else:
        print('您选择的序号不存在')
